from playwright import sync_playwright


def run(playwright):
    browser = playwright.chromium.launch(headless=False)
    context = browser.newContext()

    # Open new page
    page = context.newPage()

    page.goto("http://192.168.2.20:5400/#/login")

    # Click input[placeholder="請輸入登錄賬號"]
    page.click("input[placeholder=\"請輸入登錄賬號\"]")

    # Fill input[placeholder="請輸入登錄賬號"]
    page.fill("input[placeholder=\"請輸入登錄賬號\"]", "adin")

    # Click input[placeholder="請輸入登錄賬號"]
    page.click("input[placeholder=\"請輸入登錄賬號\"]")

    # Fill input[placeholder="請輸入登錄賬號"]
    page.fill("input[placeholder=\"請輸入登錄賬號\"]", "zhanghuan")

    # Press Tab
    page.press("input[placeholder=\"請輸入登錄賬號\"]", "Tab")

    # Fill input[placeholder="請輸入登錄密碼"]
    page.fill("input[placeholder=\"請輸入登錄密碼\"]", "123456")

    # Click //button[normalize-space(.)='登錄']
    # with page.expect_navigation(url="http://192.168.2.20:5400/#/dashboard"):
    with page.expect_navigation():
        page.click("//button[normalize-space(.)='登錄']")

    # Click span[role="button"]
    page.click("span[role=\"button\"]")

    # Click text=/.*深圳仓.*/
    page.click("text=/.*深圳仓.*/")

    # Click text="出庫管理"
    page.click("text=\"出庫管理\"")

    # Click //li[normalize-space(.)='出庫單管理' and normalize-space(@role)='menuitem']
    # with page.expect_navigation(url="http://192.168.2.20:5400/#/outStorage"):
    with page.expect_navigation():
        page.click("//li[normalize-space(.)='出庫單管理' and normalize-space(@role)='menuitem']")

    # Click text="CK2012210000017"
    page.click("text=\"CK2012210000017\"")

    # Click div[aria-label="出庫單"] >> text="取消"
    page.click("div[aria-label=\"出庫單\"] >> text=\"取消\"")

    # Click text="CK2012210000014"
    page.click("text=\"CK2012210000014\"")

    # Click //div[normalize-space(.)='訂單詳情']/button[normalize-space(@aria-label)='Close']/i
    page.click("//div[normalize-space(.)='訂單詳情']/button[normalize-space(@aria-label)='Close']/i")

    # Click //li[normalize-space(.)='訂單複覈' and normalize-space(@role)='menuitem']
    # with page.expect_navigation(url="http://192.168.2.20:5400/#/orderRecheck"):
    with page.expect_navigation():
        page.click("//li[normalize-space(.)='訂單複覈' and normalize-space(@role)='menuitem']")

    # Click text="入庫管理"
    page.click("text=\"入庫管理\"")

    # Click //li[normalize-space(.)='入庫單管理' and normalize-space(@role)='menuitem']
    # with page.expect_navigation(url="http://192.168.2.20:5400/#/warehouseReceipt"):
    with page.expect_navigation():
        page.click("//li[normalize-space(.)='入庫單管理' and normalize-space(@role)='menuitem']")

    # Click //div[2]/table/tbody/tr[1]/td[4]/div[normalize-space(.)='RK2012220003']
    page.click("//div[2]/table/tbody/tr[1]/td[4]/div[normalize-space(.)='RK2012220003']")
    # assert page.url() == "http://192.168.2.20:5400/#/"

    # Click //div[normalize-space(.)='入庫單詳情']/button[normalize-space(@aria-label)='Close']/i
    page.click("//div[normalize-space(.)='入庫單詳情']/button[normalize-space(@aria-label)='Close']/i")

    # Click //li[normalize-space(.)='上架' and normalize-space(@role)='menuitem']
    # with page.expect_navigation(url="http://192.168.2.20:5400/#/putShelf"):
    with page.expect_navigation():
        page.click("//li[normalize-space(.)='上架' and normalize-space(@role)='menuitem']")

    # Click text="打印上架單"
    page.click("text=\"打印上架單\"")

    # Click //label/span/span
    page.click("//label/span/span")

    # Click text="打印上架單"
    page.click("text=\"打印上架單\"")

    # Click //div[normalize-space(.)='訂單管理']
    page.click("//div[normalize-space(.)='訂單管理']")

    # Click //li[normalize-space(.)='客户管理' and normalize-space(@role)='menuitem']
    # with page.expect_navigation(url="http://192.168.2.20:5400/#/offlineCustomer"):
    with page.expect_navigation():
        page.click("//li[normalize-space(.)='客户管理' and normalize-space(@role)='menuitem']")

    # Click //tr[starts-with(normalize-space(.), '1C201203003vvee15632519865广东省深圳市南山区南头街道桃李路1号深圳市前海学校南山区113.91094522.530152邱锦雄2020')]/td[1]/div/label/span/span
    page.click(
        "//tr[starts-with(normalize-space(.), '1C201203003vvee15632519865广东省深圳市南山区南头街道桃李路1号深圳市前海学校南山区113.91094522.530152邱锦雄2020')]/td[1]/div/label/span/span")

    # Click text="新增客户"
    page.click("text=\"新增客户\"")

    # Click //button[normalize-space(.)='取消']
    page.click("//button[normalize-space(.)='取消']")

    # Close page
    page.close()

    # ---------------------
    context.close()
    browser.close()


with sync_playwright() as playwright:
    run(playwright)
